﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Data;
using System.Linq;

namespace JM.JCY.SharePointComponent.LawArchives.InvestigateDetails
{
    public partial class InvestigateDetailsUserControl : UserControl
    {
        T_AJBSGXDataSet.T_AJBSGXDataTable AJBSGXData;
        T_PB_AJXXDataSet.T_PB_XYRAY_DMDataTable AllXYRAYDT;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string N_BH = Request.QueryString["N_BH"].ToString();
                this.lbBH.Text = N_BH;
                var PBDT = GetPBDataByBH(Convert.ToDecimal(N_BH));
                AllXYRAYDT = GetPBXYRAYByBH(Convert.ToDecimal(N_BH));
                BindData(PBDT);
                AJBSGXData = GetAJBSGXDataByBH(Convert.ToDecimal(N_BH));

                lbBATS.Text = AJBSGXData.Count > 0 ? AJBSGXData[0].N_BATS.ToString() : "0";
                DataRow[] rows = GetPBXYRByBH(Convert.ToDecimal(N_BH)).Select();
                dlXYR.DataSource = rows;
                dlXYR.DataBind();
                if (rows.Count() > 0)
                {

                    XYRNoRecord.Visible = false;
                }

                else
                    XYRNoRecord.Visible = true;
            }
        }

        public T_AJBSGXDataSet.T_AJBSGXDataTable GetAJBSGXDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJBSGXDataByBH(BH);
            }
        }
        public T_PB_AJXXDataSet.T_PB_AJXXDataTable GetPBDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetPBDataByBH(BH);
            }
        }

        public T_PB_AJXXDataSet.T_PB_XYRDataTable GetPBXYRByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetPBXYRByBH(BH);
            }
        }

        public T_PB_AJXXDataSet.T_PB_XYRAY_DMDataTable GetPBXYRAYByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetPBXYRAYByBH(BH);
            }
        }
        public void BindData(T_PB_AJXXDataSet.T_PB_AJXXDataTable PBDT)
        {
            var row = PBDT[0];
            this.lbID.Text = string.IsNullOrEmpty(row.C_AH) ? string.Empty : row.C_AH.ToString();
            lbPerson.Text = string.IsNullOrEmpty(row.C_CBR) ? string.Empty : row.C_CBR.ToString();
            lbGetDate.Text = row.IsD_SARQNull() ? string.Empty : row.D_SARQ.ToString("yyyy年MM月dd日");
            lbPlace.Text = string.IsNullOrEmpty(row.C_TQDW) ? string.Empty : row.C_TQDW.ToString();
            lbEndDate.Text = row.IsD_JARQNull() ? string.Empty : row.D_JARQ.ToString("yyyy年MM月dd日");
        }
        protected void dlXYR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_PB_AJXXDataSet.T_PB_XYRRow row = (T_PB_AJXXDataSet.T_PB_XYRRow)e.Item.DataItem;
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbReason = (Label)e.Item.FindControl("lbReason");
            Label lbConclusionDetail = (Label)e.Item.FindControl("lbConclusionDetail");
            Label lbConclusionReason = (Label)e.Item.FindControl("lbConclusionReason");
            lbName.Text = string.IsNullOrEmpty(row.C_XM) ? string.Empty : row.C_XM;
            //提请案由
             var TQAY= from all in AllXYRAYDT
                   where all.N_LX==1 && !string.IsNullOrEmpty(row.C_XM)&&all.C_XM==row.C_XM
                   select all.C_MC;
            lbReason.Text = TQAY.Count()>0?TQAY.First():string.Empty;
            //审结案由
            var SJAY = from all in AllXYRAYDT
                       where all.N_LX == 2 && !string.IsNullOrEmpty(row.C_XM) && all.C_XM == row.C_XM
                       select all.C_MC;
            lbConclusionReason.Text = SJAY.Count() > 0 ? SJAY.First() : string.Empty;
            //审查处理情况 
            var SJCLDM = GetDMByBXH(306);
            var SJCL = from dm in SJCLDM
                       where !row.IsN_SJCLQKNull() &&dm.N_DM == row.N_SJCLQK
                       select dm.C_MC;
            lbConclusionDetail.Text = SJCL.Count() > 0 ? SJCL.First() : string.Empty;
        }

        //Method to get the data in table T_DM
        public T_DMDataSet.T_DMDataTable GetDMByBXH(decimal BXH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDMByBXH(BXH);
            }
        }
    }
}
